package com.zm.demo.core.reg;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegTest {

    @Test
    public void test(){

        String reg = "([^\\[]+)\\s([^,]+),\\sclient:\\s([^,]+),\\sserver:\\s([^,]+),\\srequest:\\s([^,]+),\\supstream:\\s([^,]+),\\shost:\\s([^,]+),\\sreferrer:\\s([^,]+)";
        String s = "2019/11/18 03:32:31 [error] 20803#0: *492368812 FastCGI sent in stderr: \"Primary script unknown\" while reading response header from upstream, client: 191.12.201.78, server: run.sports.qq.com, request: \"GET /999tst999?g_tk=1514204808&p_tk=OpWB6XOF96f2rlAApXgJE50ziHV596xlQ99lEenfZyY_ HTTP/1.1\", upstream: \"fastcgi://127.0.0.1:10000\", host: \"run.sports.qq.com\", referrer: \"\\N\"";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()){
            System.out.println("group(): " + matcher.group());
            for (int i = 0; i < 9; i++) {
                System.out.println("第"+ i +"组：" + matcher.group(i));
            }
        }
    }


    @Test
    public void testGroup2(){
//        String reg = "key([^,]+)value([^,]+)";
//        String reg = "key([^,]+)value([^,]+)";
        String reg = "key([^#]+)value([^:]+)";
        String s = "keyaaavalue123";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()){
            System.out.println("group(): " + matcher.group());
            for (int i = 0; i < 3; i++) {
                System.out.println("第"+ i +"组：" + matcher.group(i));
            }
        }
    }
}
